package com.eff.android.fragment.options;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.eff.android.R;

import com.eff.android.api.model.ModelSubject;
import com.eff.android.api.model.Setting;
import com.eff.android.fragment.EBaseFragment;
import com.eff.android.lib.helper.EDatabaseHelper;


public class EMainFragmentOptionsSubjects extends EBaseFragment{
	
	public static final String TAG = EMainFragmentOptionsSubjects.class.getSimpleName();
	
	RadioGroup group;
	
	public EMainFragmentOptionsSubjects(Bundle arguments) {
		super(arguments);
	}

	public static Bundle getArgs() {
		Bundle b = new Bundle();
		b.putString(TAG, TAG);
		return b;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		List<ModelSubject> models_subject = EDatabaseHelper.getInstance(getActivity().getApplicationContext()).getAllModelsSubject();
		
		for (int i = 0; i < models_subject.size(); i++) {
			Setting.newInstance().addSubject(models_subject.get(i));
		}
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
		View v = inflater.inflate(R.layout.e_frm_main_options_subjects, container, false);
		initGroupRadioButtonView(v);
		setGroupRadioButtonChanged();
		    
		return v;
    }
	
	private void initGroupRadioButtonView(View v){
		
		int size = Setting.newInstance().getListSubject().size();	
		final RadioButton[] rb = new RadioButton[size];
		group = (RadioGroup)v.findViewById(R.id.radioGroupSubject);
		group.setOrientation(RadioGroup.VERTICAL);
	    
		InitRadioButtonInGroup(rb, size);
	    InitCheckedButtonInGroup();
	}
	
	private void InitRadioButtonInGroup(RadioButton[] rb, int size){
		rb[0]  = new RadioButton(getActivity());
        rb[0].setText("Mixed");
        group.addView(rb[0]); //the RadioButtons are added to the radioGroup instead of the layout
        
	    for (int i = 1; i < size; i++) {
	    	rb[i]  = new RadioButton(getActivity());
	        rb[i].setText(Setting.newInstance().getListSubject().get(i).getSubjectName());
	        group.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
	    }
	}
	
	private void InitCheckedButtonInGroup(){
		String currentSubject = Setting.newInstance().getSubjectName().toLowerCase();
		
		for (int i=0; i<group.getChildCount(); i++){
			RadioButton radioBtn = (RadioButton) group.getChildAt(i);
			
			if (radioBtn.getText().toString().toLowerCase().equals(currentSubject)){
				group.check(radioBtn.getId());
				break;
			}
		}
	}
	
	private void setGroupRadioButtonChanged(){
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				setSubjects(checkedId);
			}
		});
	}
	
	private void setSubjects (int checkedId){
		String radioBtnText = ((RadioButton)group.findViewById(checkedId))
				.getText().toString().toLowerCase();
		Setting.newInstance().setSubject(radioBtnText);
	}

}